package org.yaac.server.egql.evaluator;

import static com.google.common.collect.Sets.newHashSet;

import java.util.HashSet;
import java.util.Set;

import org.yaac.server.egql.EntityProperty;
import org.yaac.server.egql.exception.EGQLException;
import org.yaac.server.egql.processor.ProcessData.ProcessDataRecord;
import org.yaac.server.util.DatastoreUtil;



/**
 * @author Max Zhu (thebbsky@gmail.com)
 *
 */
public class BetweenEvaluator extends Evaluator {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private final Evaluator op;
	
	private final Evaluator op1;
	
	private final Evaluator op2;
	
	/**
	 * true --> between
	 * false --> not between
	 */
	private final boolean betweenFlag;
	
	/**
	 * @param op
	 * @param op1
	 * @param op2
	 * @param betweenFlag
	 */
	public BetweenEvaluator(Evaluator op, Evaluator op1, Evaluator op2, boolean betweenFlag) {
		super();
		this.op = op;
		this.op1 = op1;
		this.op2 = op2;
		this.betweenFlag = betweenFlag;
	}

	@Override
	public EvaluationResult evaluate(ProcessDataRecord record) {
		EvaluationResult r = op.evaluate(record);
		EvaluationResult r1 = op1.evaluate(record);
		EvaluationResult r2 = op2.evaluate(record);
		
		if (betweenFlag) {
			return new EvaluationResult(between(r.getPayload(), r1.getPayload(), r2.getPayload()), 
					r, r1, r2).withTitle(this.getText());	
		} else {
			return new EvaluationResult(!between(r.getPayload(), r1.getPayload(), r2.getPayload()), 
					r, r1, r2).withTitle(this.getText());
		}
	}
	
	private boolean between(Object val, Object from, Object to) {
		// between is inclusive
		return DatastoreUtil.deterministicCompare(val, from) >= 0 && DatastoreUtil.deterministicCompare(val, to) <= 0;
	}
	
	@Override
	public Set<AggregationEvaluator> aggregationChildren() {
		Set<AggregationEvaluator> result = newHashSet();
		result.addAll(this.op.aggregationChildren());
		result.addAll(this.op1.aggregationChildren());
		result.addAll(this.op2.aggregationChildren());
		return result;
	}

	@Override
	public Set<EntityProperty> nonAggregationProperties() {
		Set<EntityProperty> result = new HashSet<EntityProperty>();
		result.addAll(this.op.nonAggregationProperties());
		result.addAll(this.op1.nonAggregationProperties());
		result.addAll(this.op2.nonAggregationProperties());
		return result;
	}

	@Override
	public void validate() throws EGQLException {
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = super.hashCode();
		result = prime * result + (betweenFlag ? 1231 : 1237);
		result = prime * result + ((op == null) ? 0 : op.hashCode());
		result = prime * result + ((op1 == null) ? 0 : op1.hashCode());
		result = prime * result + ((op2 == null) ? 0 : op2.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (!super.equals(obj))
			return false;
		if (getClass() != obj.getClass())
			return false;
		BetweenEvaluator other = (BetweenEvaluator) obj;
		if (betweenFlag != other.betweenFlag)
			return false;
		if (op == null) {
			if (other.op != null)
				return false;
		} else if (!op.equals(other.op))
			return false;
		if (op1 == null) {
			if (other.op1 != null)
				return false;
		} else if (!op1.equals(other.op1))
			return false;
		if (op2 == null) {
			if (other.op2 != null)
				return false;
		} else if (!op2.equals(other.op2))
			return false;
		return true;
	}

	@Override
	public String toString() {
		return "BetweenEvaluator [op=" + op + ", op1=" + op1 + ", op2=" + op2 + ", betweenFlag=" + betweenFlag + "]";
	}
}
